package com.hskj.springboot.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;


public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
//    @Override
//    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("message", "登录失败");
//        map.put("code", 500);
//        //响应的类型
//        response.setContentType("application/json;charset=UTF-8");
//        //将map集合转换为json字符串
//        String s = new ObjectMapper().writeValueAsString(map);
//        response.getWriter().println(s);
//    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("message", "登录失败");
        map.put("code", 500);
        //响应的类型
        response.setContentType("application/json;charset=UTF-8");
        //将map集合转换为json字符串
        String s = new ObjectMapper().writeValueAsString(map);
        response.getWriter().println(s);
    }
}
